home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / gr564s.zip / SRC / RCSDIFF.C < prev    next >
C/C++ Source or Header  |  1992-09-05  |  12KB  |  450 lines

  1. /*
  2.  *                     RCS rcsdiff operation
  3.  */
  4. /*****************************************************************************
  5.  *                       generate difference between RCS revisions
  6.  *****************************************************************************
  7.  */
  8.  
  9. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  10.    Copyright 1990, 1991, 1992 by Paul Eggert
  11.    Distributed under license by the Free Software Foundation, Inc.
  12.  
  13. This file is part of RCS.
  14.  
  15. RCS is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation; either version 2, or (at your option)
  18. any later version.
  19.  
  20. RCS is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with RCS; see the file COPYING.  If not, write to
  27. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  28.  
  29. Report problems and direct all questions to:
  30.  
  31.     rcs-bugs@cs.purdue.edu
  32.  
  33. */
  34.  
  35.  
  36.  
  37.  
  38. /* $Log: rcsdiff.c,v $
  39.  * Revision 5.14  1992/07/28  16:12:44  eggert
  40.  * Add -V.  Use co -M for better dates with traditional diff -c.
  41.  *
  42.  * Revision 5.13  1992/02/17  23:02:23  eggert
  43.  * Output more readable context diff headers.
  44.  * Suppress needless checkout and comparison of identical revisions.
  45.  *
  46.  * Revision 5.12  1992/01/24  18:44:19  eggert
  47.  * Add GNU diff 1.15.2's new options.  lint -> RCS_lint
  48.  *
  49.  * Revision 5.11  1992/01/06  02:42:34  eggert
  50.  * Update usage string.
  51.  *
  52.  * Revision 5.10  1991/10/07  17:32:46  eggert
  53.  * Remove lint.
  54.  *
  55.  * Revision 5.9  1991/08/19  03:13:55  eggert
  56.  * Add RCSINIT, -r$.  Tune.
  57.  *
  58.  * Revision 5.8  1991/04/21  11:58:21  eggert
  59.  * Add -x, RCSINIT, MS-DOS support.
  60.  *
  61.  * Revision 5.7  1990/12/13  06:54:07  eggert
  62.  * GNU diff 1.15 has -u.
  63.  *
  64.  * Revision 5.6  1990/11/01  05:03:39  eggert
  65.  * Remove unneeded setid check.
  66.  *
  67.  * Revision 5.5  1990/10/04  06:30:19  eggert
  68.  * Accumulate exit status across files.
  69.  *
  70.  * Revision 5.4  1990/09/27  01:31:43  eggert
  71.  * Yield 1, not EXIT_FAILURE, when diffs are found.
  72.  *
  73.  * Revision 5.3  1990/09/11  02:41:11  eggert
  74.  * Simplify -kkvl test.
  75.  *
  76.  * Revision 5.2  1990/09/04  17:07:19  eggert
  77.  * Diff's argv was too small by 1.
  78.  *
  79.  * Revision 5.1  1990/08/29  07:13:55  eggert
  80.  * Add -kkvl.
  81.  *
  82.  * Revision 5.0  1990/08/22  08:12:46  eggert
  83.  * Add -k, -V.  Don't use access().  Add setuid support.
  84.  * Remove compile-time limits; use malloc instead.
  85.  * Don't pass arguments with leading '+' to diff; GNU DIFF treats them as options.
  86.  * Add GNU diff's flags.  Make lock and temp files faster and safer.
  87.  * Ansify and Posixate.
  88.  *
  89.  * Revision 4.6  89/05/01  15:12:27  narten
  90.  * changed copyright header to reflect current distribution rules
  91.  * 
  92.  * Revision 4.5  88/08/09  19:12:41  eggert
  93.  * Use execv(), not system(); yield exit status like diff(1)s; allow cc -R.
  94.  * 
  95.  * Revision 4.4  87/12/18  11:37:46  narten
  96.  * changes Jay Lepreau made in the 4.3 BSD version, to add support for
  97.  * "-i", "-w", and "-t" flags and to permit flags to be bundled together,
  98.  * merged in.
  99.  * 
  100.  * Revision 4.3  87/10/18  10:31:42  narten
  101.  * Updating version numbers. Changes relative to 1.1 actually
  102.  * relative to 4.1
  103.  * 
  104.  * Revision 1.3  87/09/24  13:59:21  narten
  105.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  106.  * warnings)
  107.  * 
  108.  * Revision 1.2  87/03/27  14:22:15  jenkins
  109.  * Port to suns
  110.  * 
  111.  * Revision 4.1  83/05/03  22:13:19  wft
  112.  * Added default branch, option -q, exit status like diff.
  113.  * Added fterror() to replace faterror().
  114.  * 
  115.  * Revision 3.6  83/01/15  17:52:40  wft
  116.  * Expanded mainprogram to handle multiple RCS files.
  117.  *
  118.  * Revision 3.5  83/01/06  09:33:45  wft
  119.  * Fixed passing of -c (context) option to diff.
  120.  *
  121.  * Revision 3.4  82/12/24  15:28:38  wft
  122.  * Added call to catchsig().
  123.  *
  124.  * Revision 3.3  82/12/10  16:08:17  wft
  125.  * Corrected checking of return code from diff; improved error msgs.
  126.  *
  127.  * Revision 3.2  82/12/04  13:20:09  wft
  128.  * replaced getdelta() with gettree(). Changed diagnostics.
  129.  *
  130.  * Revision 3.1  82/11/28  19:25:04  wft
  131.  * Initial revision.
  132.  *
  133.  */
  134. #include "rcsbase.h"
  135.  
  136. #if DIFF_L
  137. static char const *setup_label P((struct buf*,char const*,char const[datesize]));
  138. #endif
  139. static void cleanup P((void));
  140.  
  141. static int exitstatus;
  142. static RILE *workptr;
  143. static struct stat workstat;
  144.  
  145. mainProg(rcsdiffId, "rcsdiff", "$Id: rcsdiff.c,v 5.14 1992/07/28 16:12:44 eggert Exp $")
  146. {
  147.     static char const cmdusage[] =
  148.         "\nrcsdiff usage: rcsdiff [-ksubst] [-q] [-rrev1 [-rrev2]] [-Vn] [-xsuff] [diff options] file ...";
  149.  
  150.     int argc_min;
  151.     int  revnums;                 /* counter for revision numbers given */
  152.     char const *rev1, *rev2;    /* revision numbers from command line */
  153.     char const *xrev1, *xrev2;    /* expanded revision numbers */
  154.     char const *expandarg, *lexpandarg, *versionarg;
  155. #if DIFF_L
  156.     static struct buf labelbuf[2];
  157.     int file_labels;
  158.     char const **diff_label1, **diff_label2;
  159.     char date2[datesize];
  160. #endif
  161.     char const *cov[9 + !DIFF_L];
  162.     char const **diffv, **diffp;    /* argv for subsidiary diff */
  163.     char const **pp, *p, *diffvstr;
  164.     struct buf commarg;
  165.     struct buf numericrev;    /* expanded revision number */
  166.     struct hshentries *gendeltas;    /* deltas to be generated */
  167.     struct hshentry * target;
  168.     char *a, *dcp, **newargv;
  169.     int no_diff_means_no_output;
  170.     register c;
  171.  
  172.     exitstatus = DIFF_SUCCESS;
  173.  
  174.     bufautobegin(&commarg);
  175.     bufautobegin(&numericrev);
  176.     argc_min = 1;
  177.     revnums = 0;
  178.     rev1 = rev2 = xrev2 = nil;
  179. #if DIFF_L
  180.     file_labels = 0;
  181. #endif
  182.     expandarg = versionarg = 0;
  183.     no_diff_means_no_output = true;
  184.     suffixes = X_DEFAULT;
  185.  
  186.     /* Room for args + 2 i/o [+ 2 labels] + 1 file + 1 trailing null.  */
  187.     diffp = diffv = tnalloc(char const*, argc + 4 + 2*DIFF_L);
  188.     *diffp++ = nil;
  189.     *diffp++ = nil;
  190.     *diffp++ = DIFF;
  191.  
  192.     argc = getRCSINIT(argc, argv, &newargv);
  193.     argv = newargv;
  194.     while (a = *++argv,  0<--argc && *a++=='-') {
  195.     dcp = a;
  196.     while (c = *a++) switch (c) {
  197.         case 'r':
  198.             switch (++revnums) {
  199.             case 1: rev1=a; break;
  200.             case 2: rev2=a; break;
  201.             default: faterror("too many revision numbers");
  202.             }
  203.             goto option_handled;
  204.         case '-': case 'D':
  205.             no_diff_means_no_output = false;
  206.             /* fall into */
  207.         case 'C': case 'F': case 'I': case 'L': case 'W':
  208. #if DIFF_L
  209.             if (c == 'L'  &&  ++file_labels == 2)
  210.             faterror("too many -L options");
  211. #endif
  212.             *dcp++ = c;
  213.             if (*a)
  214.             do *dcp++ = *a++;
  215.             while (*a);
  216.             else {
  217.             if (!--argc)
  218.                 faterror("-%c needs following argument%s",
  219.                     c, cmdusage
  220.                 );
  221.             *diffp++ = *argv++;
  222.             }
  223.             break;
  224.         case 'y':
  225.             no_diff_means_no_output = false;
  226.             /* fall into */
  227.         case 'B': case 'H': case 'T':
  228.         case '0': case '1': case '2': case '3': case '4':
  229.         case '5': case '6': case '7': case '8': case '9':
  230.         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  231.         case 'h': case 'i': case 'n': case 'p':
  232.         case 't': case 'u': case 'w':
  233.             *dcp++ = c;
  234.             break;
  235.         case 'q':
  236.             quietflag=true;
  237.             break;
  238.         case 'x':
  239.             suffixes = *argv + 2;
  240.             goto option_handled;
  241.         case 'V':
  242.             versionarg = *argv;
  243.             argc_min = setRCSversion(versionarg);
  244.             goto option_handled;
  245.         case 'k':
  246.             expandarg = *argv;
  247.             if (0 <= str2expmode(expandarg+2))
  248.             goto option_handled;
  249.             /* fall into */
  250.         default:
  251.             faterror("unknown option: %s%s", *argv, cmdusage);
  252.         };
  253.       option_handled:
  254.     if (dcp != *argv+1) {
  255.         *dcp = 0;
  256.         *diffp++ = *argv;
  257.     }
  258.     } /* end of option processing */
  259.  
  260.     if (argc < argc_min)
  261.         faterror("no input file%s", cmdusage);
  262.  
  263.     for (pp = diffv+3, c = 0;  pp<diffp;  )
  264.         c += strlen(*pp++) + 1;
  265.     diffvstr = a = tnalloc(char, c + 1);
  266.     for (pp = diffv+3;  pp<diffp;  ) {
  267.         p = *pp++;
  268.         *a++ = ' ';
  269.         while ((*a = *p++))
  270.             a++;
  271.     }
  272.     *a = 0;
  273.  
  274. #if DIFF_L
  275.     diff_label1 = diff_label2 = nil;
  276.     if (file_labels < 2) {
  277.         if (!file_labels)
  278.             diff_label1 = diffp++;
  279.         diff_label2 = diffp++;
  280.     }
  281. #endif
  282.     diffp[2] = nil;
  283.  
  284.     cov[0] = 0;
  285.     cov[2] = CO;
  286.     cov[3] = "-q";
  287. #   if !DIFF_L
  288.     cov[4] = "-M";
  289. #   endif
  290.  
  291.     /* Now handle all pathnames.  */
  292.     for (;  0 < argc;  cleanup(), ++argv, --argc) {
  293.         ffree();
  294.  
  295.         if (pairnames(argc, argv, rcsreadopen, true, false)  <=  0)
  296.             continue;
  297.         diagnose("===================================================================\nRCS file: %s\n",RCSname);
  298.         if (!rev2) {
  299.         /* Make sure work file is readable, and get its status.  */
  300.         if (!(workptr = Iopen(workname, FOPEN_R_WORK, &workstat))) {
  301.             eerror(workname);
  302.             continue;
  303.         }
  304.         }
  305.  
  306.  
  307.         gettree(); /* reads in the delta tree */
  308.  
  309.         if (Head==nil) {
  310.             error("no revisions present");
  311.             continue;
  312.         }
  313.         if (revnums==0  ||  !*rev1)
  314.             rev1  =  Dbranch ? Dbranch : Head->num;
  315.  
  316.         if (!fexpandsym(rev1, &numericrev, workptr)) continue;
  317.         if (!(target=genrevs(numericrev.string,(char *)nil,(char *)nil,(char *)nil,&gendeltas))) continue;
  318.         xrev1=target->num;
  319. #if DIFF_L
  320.         if (diff_label1)
  321.         *diff_label1 = setup_label(&labelbuf[0], target->num, target->date);
  322. #endif
  323.  
  324.         lexpandarg = expandarg;
  325.         if (revnums==2) {
  326.             if (!fexpandsym(
  327.                 *rev2 ? rev2  : Dbranch ? Dbranch  : Head->num,
  328.                 &numericrev,
  329.                 workptr
  330.             ))
  331.             continue;
  332.             if (!(target=genrevs(numericrev.string,(char *)nil,(char *)nil,(char *)nil,&gendeltas))) continue;
  333.             xrev2=target->num;
  334.             if (no_diff_means_no_output  &&  xrev1 == xrev2)
  335.             continue;
  336.         } else if (
  337.             target->lockedby
  338.         &&    !lexpandarg
  339.         &&    Expand == KEYVAL_EXPAND
  340.         &&    WORKMODE(RCSstat.st_mode,true) == workstat.st_mode
  341.         )
  342.             lexpandarg = "-kkvl";
  343.         Izclose(&workptr);
  344. #if DIFF_L
  345.         if (diff_label2)
  346.         if (revnums == 2)
  347.             *diff_label2 = setup_label(&labelbuf[1], target->num, target->date);
  348.         else {
  349.             time2date(workstat.st_mtime, date2);
  350.             *diff_label2 = setup_label(&labelbuf[1], (char*)0, date2);
  351.         }
  352. #endif
  353.  
  354.         diagnose("retrieving revision %s\n", xrev1);
  355.         bufscpy(&commarg, "-p");
  356.         bufscat(&commarg, xrev1);
  357.  
  358.         cov[1] = diffp[0] = maketemp(0);
  359.         pp = &cov[4 + !DIFF_L];
  360.         *pp++ = commarg.string;
  361.         if (lexpandarg)
  362.             *pp++ = lexpandarg;
  363.         if (versionarg)
  364.             *pp++ = versionarg;
  365.         *pp++ = RCSname;
  366.         *pp = 0;
  367.  
  368.         if (runv(cov)) {
  369.             error("co failed");
  370.             continue;
  371.         }
  372.         if (!rev2) {
  373.             diffp[1] = workname;
  374.             if (workname[0] == '+') {
  375.             /* Some diffs have options with leading '+'.  */
  376.             char *dp = ftnalloc(char, strlen(workname)+3);
  377.             diffp[1] = dp;
  378.             *dp++ = '.';
  379.             *dp++ = SLASH;
  380.             VOID strcpy(dp, workname);
  381.             }
  382.         } else {
  383.             diagnose("retrieving revision %s\n",xrev2);
  384.             bufscpy(&commarg, "-p");
  385.             bufscat(&commarg, xrev2);
  386.             cov[1] = diffp[1] = maketemp(1);
  387.             cov[4 + !DIFF_L] = commarg.string;
  388.             if (runv(cov)) {
  389.                 error("co failed");
  390.                 continue;
  391.             }
  392.         }
  393.         if (!rev2)
  394.             diagnose("diff%s -r%s %s\n", diffvstr, xrev1, workname);
  395.         else
  396.             diagnose("diff%s -r%s -r%s\n", diffvstr, xrev1, xrev2);
  397.  
  398.         switch (runv(diffv)) {
  399.             case DIFF_SUCCESS:
  400.                 break;
  401.             case DIFF_FAILURE:
  402.                 if (exitstatus == DIFF_SUCCESS)
  403.                     exitstatus = DIFF_FAILURE;
  404.                 break;
  405.             default:
  406.                 error("diff failed");
  407.         }
  408.     }
  409.  
  410.     tempunlink();
  411.     exitmain(exitstatus);
  412. }
  413.  
  414.     static void
  415. cleanup()
  416. {
  417.     if (nerror) exitstatus = DIFF_TROUBLE;
  418.     Izclose(&finptr);
  419.     Izclose(&workptr);
  420. }
  421.  
  422. #if RCS_lint
  423. #    define exiterr rdiffExit
  424. #endif
  425.     exiting void
  426. exiterr()
  427. {
  428.     tempunlink();
  429.     _exit(DIFF_TROUBLE);
  430. }
  431.  
  432. #if DIFF_L
  433.     static char const *
  434. setup_label(b, num, date)
  435.     struct buf *b;
  436.     char const *num;
  437.     char const date[datesize];
  438. {
  439.     char *p;
  440.     char datestr[datesize];
  441.     VOID date2str(date, datestr);
  442.     bufalloc(b, strlen(workname) + datesize+3 + (num?strlen(num)+1:0));
  443.     p = b->string;
  444.     VOID sprintf(p, "-L%s\t%s", workname, datestr);
  445.     if (num)
  446.         VOID sprintf(p + strlen(p), "\t%s", num);
  447.     return p;
  448. }
  449. #endif
  450.